package com.houxm.jbapu;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class TableUtils {

	public void bundle() throws ClassNotFoundException, SQLException {
		Connection conn = this.getConfig().getConn();
		DatabaseMetaData dmd = conn.getMetaData();
		ResultSet rs = dmd.getCatalogs();
		while (rs.next()) {
			// 获得数据库名称
			String dbName = rs.getString("TABLE_CAT");
			// 跳过MySql的默认系统表
			if("mysql".contains(dbName.toLowerCase()) || "information_schema".contains(dbName.toLowerCase()) || "performance_schema".contains(dbName.toLowerCase())) {
				continue;
			}
			// 获得某一数据库中所有的表结构
			ResultSet tableRs = dmd.getTables(dbName, dbName, null, new String[] { "TABLE" });
			while (tableRs.next()) {
				String tableName = tableRs.getString("TABLE_NAME");
				Table t = new Table(dbName, tableName);
//				String tableType = tableRs.getString("TABLE_TYPE");
				// 获得表中所有的字段名称
				Set<String> set = new HashSet<String>();
				ResultSet colSet = dmd.getColumns(dbName, "%", tableName, "%");
				while (colSet.next()) {
//					columnName = colRet.getString("COLUMN_NAME"); 
//					columnType = colRet.getString("TYPE_NAME"); 
//					int datasize = colRet.getInt("COLUMN_SIZE"); 
//					int digits = colRet.getInt("DECIMAL_DIGITS"); 
//					int nullable = colRet.getInt("NULLABLE"); 
					set.add(colSet.getString("COLUMN_NAME"));
				}
				t.setColumnNames(set);
				// 获得表的所有关键字字段名称
				ResultSet pkRSet = dmd.getPrimaryKeys(dbName, null, tableName);
				List<String> pk = new ArrayList<String>();
				while (pkRSet.next()) {
//					System.err.println("TABLE_CAT : " + pkRSet.getObject(1));
//					System.err.println("TABLE_SCHEM: " + pkRSet.getObject(2));
//					System.err.println("TABLE_NAME : " + pkRSet.getObject(3));
//					System.err.println("COLUMN_NAME: " + pkRSet.getObject(4));
//					System.err.println("KEY_SEQ : " + pkRSet.getObject(5));
//					System.err.println("PK_NAME : " + pkRSet.getObject(6));
					pk.add(pkRSet.getString(4));
				}
				t.setPrimaryKey(pk);
				Tables.o.putTable(tableName, t);
				DBUtils.close(pkRSet);
			}
			DBUtils.close(tableRs);
		}
		DBUtils.close(rs);
		DBUtils.close(conn);
	}

	private Config getConfig() {
		return new Config();
	}

	public static void main(String[] args) {
		try {
			TableUtils tu = new TableUtils();
			Connection conn = tu.getConfig().getConn();
			DatabaseMetaData dmd = conn.getMetaData();

			ResultSet rs = dmd.getCatalogs();
			while (rs.next()) {
				String name = rs.getString("TABLE_CAT");
				String name2 = rs.getString(1);
				System.out.println(name + "," + name2);
			}

			rs = dmd.getTables("mindata", "mindata", null, new String[] { "TABLE" });
			while (rs.next()) {
				String name = rs.getString("TABLE_NAME");
				String name2 = rs.getString("TABLE_TYPE");
				System.out.println(name + "," + name2);
			}
			ResultSet pkRSet = dmd.getPrimaryKeys("mindata", null, "test");
			while (pkRSet.next()) {
				System.err.println("****** Comment ******");
				System.err.println("TABLE_CAT : " + pkRSet.getObject(1));
				System.err.println("TABLE_SCHEM: " + pkRSet.getObject(2));
				System.err.println("TABLE_NAME : " + pkRSet.getObject(3));
				System.err.println("COLUMN_NAME: " + pkRSet.getObject(4));
				System.err.println("KEY_SEQ : " + pkRSet.getObject(5));
				System.err.println("PK_NAME : " + pkRSet.getObject(6));
				System.err.println("****** ******* ******");
			}

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
